package lambdademo;




import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.Consumer;

@FunctionalInterface
interface IA {
    void teat();
    default void teat2() {
        System.out.println("default方法");
    }
}

//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}

//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}

class T {
    public void test() {
        System.out.println("test的方法");
    }
}
public class Test {
    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("abc",3);
        map.put("world",5);
        map.put("hello",4);

        map.forEach((key,val) -> System.out.println("key: " + key + " val: " + val));
    }

    public static void main11(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("abcd");
        arrayList.add("world");
        arrayList.add("hello");

        arrayList.sort((o1,o2) -> o1.compareTo(o2));

        arrayList.forEach(s -> System.out.println(s));
    }

    public static void main10(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("abcd");
        arrayList.add("world");
        arrayList.add("hello");

        arrayList.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                //简单遍历集合中的所有元素
                System.out.println(s);
            }
        });

        System.out.println("================");
        //Lambda表达式:
        arrayList.forEach(s -> System.out.println(s));
    }

    public static void main9(String[] args) {
        //Lambda表达式的变量捕获
        int count = 20;
        //count = 10;
        MoreParameterNoReturn moreParameterNoReturn = (int a,int b) -> {
            System.out.println(count);
            System.out.println(a + b);
        };
    }

    public static void main8(String[] args) {
        int a = 10;
        //a = 9;
        new T() {
            @Override
            public void test() {
                System.out.println("重写了 test 这个方法");
                System.out.println("捕获变量：" + a);
            }
        };

    }

    public static void main7(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        //可以替换为Lambda表达式:
        PriorityQueue<Integer> priorityQueue1 = new PriorityQueue<>(((o1, o2) -> o1.compareTo(o2)));
    }

    public static void main6(String[] args) {
        //MoreParameterReturn moreParameterReturn = (int a,int b) -> {return a * b;};
        //简化后：
        MoreParameterReturn moreParameterReturn = (a,b) ->  a * b;
        System.out.println(moreParameterReturn.test(30, 2));
    }

    public static void main5(String[] args) {
        //有返回值一个参数
        //OneParameterReturn oneParameterReturn = (int a) -> {return a + 10;};
        //简化后：
        OneParameterReturn oneParameterReturn = (a) -> a + 10;
        System.out.println(oneParameterReturn.test(20));
    }

    public static void main4(String[] args) {
        //有返回值无参数
        //NoParameterReturn noParameterReturn = () -> {return 20;};
        //简化后：
        NoParameterReturn noParameterReturn = () -> 20;
        System.out.println(noParameterReturn.test());
    }

    public static void main3(String[] args) {
        //无返回值多个参数
        //MoreParameterNoReturn moreParameterNoReturn = (int a,int b) -> {System.out.println(a+b);};
        //简化后：
        MoreParameterNoReturn moreParameterNoReturn = (a,b) -> System.out.println(a+b);
        moreParameterNoReturn.test(10,20);
    }

    public static void main2(String[] args) {
        //无返回值一个参数
        //OneParameterNoReturn oneParameterNoReturn = (int a) -> {System.out.println(a);};
        //简化后：
        OneParameterNoReturn oneParameterNoReturn = a -> System.out.println(a);
        oneParameterNoReturn.test(20);
    }

    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn1 = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("test的重写方法");
            }
        };
        noParameterNoReturn1.test();

        System.out.println("==================");
        //无返回值无参数

        NoParameterNoReturn noParameterNoReturn = () -> {System.out.println("test的重写方法");};
        noParameterNoReturn.test();
    }
}
